package com.framework.seedkit.utils.encrypt;

import android.annotation.SuppressLint;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


public class RSA {
    public static final String KEY_ALGORITHM_DECRYPT = "RSA/ECB/PKCS1Padding";
    public static final String KEY_ALGORITHM = "RSA";
    
    /**
     * 生成公私密钥对
     * @throws NoSuchAlgorithmException 
     */
    @SuppressLint("TrulyRandom")
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        
        // 密钥位数
        keyPairGen.initialize(1024,new SecureRandom());
        
        // 密钥对
        return keyPairGen.generateKeyPair();
    }

    /**
     * 用私钥解密
     * @param encryptedData 经过公钥加密的密文
     * @param privateKey 私钥
     * @return 通过私钥解密出来的原始数据
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeyException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     * @throws UnsupportedEncodingException 
     */
    public static String decryptByPrivateKey(byte[] encryptedData, PrivateKey privateKey) throws 
        NoSuchAlgorithmException, NoSuchPaddingException, 
        InvalidKeyException, IllegalBlockSizeException, 
        BadPaddingException, UnsupportedEncodingException {
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        
        // 解密为byte数组，应该为字符串数组最后转化为字符串
        byte[] deBytes = cipher.doFinal(encryptedData);
        
        return new String(deBytes,"utf-8");
    }
    
    /**
     * 用公钥加密
     * @param originData 原始数据
     * @param publicKey 公钥
     * @return 通过公钥加密的
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     * @throws UnsupportedEncodingException 
     * @throws InvalidKeyException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException
     */
    public static byte [] encryptByPublicKey(String originData, PublicKey publicKey) throws 
        NoSuchAlgorithmException, NoSuchPaddingException, 
        UnsupportedEncodingException, InvalidKeyException,
        IllegalBlockSizeException, BadPaddingException {
        // 实例化加解密类
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);

        // 明文
        byte[] plainText = originData.getBytes("gbk");

        // 加密
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        //将明文转化为根据公钥加密的密文，为byte数组格式
        return cipher.doFinal(plainText);
    }
    
    /**
     * 通过字符串生成 PublicKey
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeySpecException 
     */
    public static PublicKey generatorPubKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
        PublicKey ret = null;
        KeyFactory keyFactory = null;
        
        keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec((Base64.decode(publicKey, Base64.DEFAULT)));
        ret = keyFactory.generatePublic(keySpec);
        
        return ret;
    }
    
    /**
     * 通过字符串生成 PrivateKey
     * @param privateKey
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeySpecException 
     */
    public static PrivateKey generatePrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
        PrivateKey ret = null;
        KeyFactory keyFactory = null;
        
        keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec((Base64.decode(privateKey, Base64.DEFAULT)));
        ret = keyFactory.generatePrivate(keySpec);
            
        return ret;
    }

    /**
     * 描述: Base64编码加密
     */
    public static String encode(byte[] data) {
        return Base64.encodeToString(data, Base64.DEFAULT);
    }

    /**
     * 描述: Base64编码解密
     */
    public static byte[] decode(String data) {
        return Base64.decode(data, Base64.DEFAULT);
    }

}
